package com.android.alarmbuy.utils;


import android.content.Context;
import android.content.SharedPreferences;

import java.util.Set;

/**
 * SharePreferences操作工具类
 */
public class SharePreferenceUtil {

    private static final String SP_NAME = "sp_red_packet";

    private static SharedPreferences sp;

    /**
     * 清除SP
     */
    public static void clear(Context context) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().clear().apply();
    }

    /**
     * 保存用户名
     */
    public static void saveUsername(Context context, String username) {
        saveString(context, "username", username);
    }

    /**
     * 获取用户名
     */
    public static String getUsername(Context context) {
        return getString(context, "username", "");
    }



    /**
     * 保存用户sto
     */
    public static void saveUserSto(Context context, String usernSto) {
        saveString(context, "user_sto", usernSto);
    }

    /**
     * 获取用户sto
     */
    public static String getUserSto(Context context) {
        return getString(context, "user_sto", "");
    }

    /**
     * 保存密码
     */
    public static void savePassword(Context context, String password) {
        saveString(context, "password", password);
    }

    /**
     * 获取密码
     */
    public static String getPassword(Context context) {
        return getString(context, "password", "");
    }

    /**
     * 是否第一次安装,第一次安装播放展示页,否则不展示
     */
    public static void saveIsFirstInstall(Context context, boolean isOpened){
        saveBoolean(context,"is_opended_2",isOpened);
    }

    public static boolean getIsFirstInstall(Context context) {
        return getBoolean(context, "is_opended_2", false);
    }

    /**
     * 是否记住密码
     */
    public static void saveIsRememberPwd(Context context, boolean IsRememberPwd){
        saveBoolean(context,"is_remember_pwd",IsRememberPwd);
    }

    public static boolean getIsRememberPwd(Context context) {
        return getBoolean(context, "is_remember_pwd", false);
    }

    /**
     * 获取SetString
     */
    public static Set<String> getStringSet(Context context, String key, Set<String> defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        return sp.getStringSet(key, defValue);
    }

    /**
     * 存SetString
     */
    public static void saveStringSet(Context context, String key, Set<String> defValue) {
        if (sp == null) {
            sp = context.getSharedPreferences(SP_NAME, 0);
        }
        sp.edit().putStringSet(key, defValue).apply();
    }

    /**
     * 获取布尔值
     */
    public static boolean getBoolean(Context context, String key,
                                     boolean defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getBoolean(key, defValue);
    }

    /**
     * 保存布尔值
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putBoolean(key, value).apply();
    }

    /**
     * 获取int值
     */
    public static int getInt(Context context, String key, int defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);

        return sp.getInt(key, defValue);
    }

    /**
     * 保存int型
     */
    public static void saveInt(Context context, String key, int value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putInt(key, value).apply();
    }

    /**
     * 获取字符值
     */
    public static String getString(Context context, String key, String defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getString(key, defValue);
    }

    /**
     * 保存字符串
     */
    public static void saveString(Context context, String key, String value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putString(key, value).apply();

    }

    /**
     * 获取long值
     */
    public static long getLong(Context context, String key, long defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getLong(key, defValue);
    }

    /**
     * 保存long型
     */
    public static void saveLong(Context context, String key, long value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putLong(key, value).apply();
    }

    /**
     * 获取float值
     */
    public static float getFloat(Context context, String key, float defValue) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        return sp.getFloat(key, defValue);
    }

    /**
     * 保存float型
     */
    private static void saveFloat(Context context, String key, float value) {
        if (sp == null)
            sp = context.getSharedPreferences(SP_NAME, 0);
        sp.edit().putFloat(key, value).apply();
    }


}
